<?php
class L_Error
{
	private $errortype;
	private $user_errors;
	private $logPath;
	public function __construct() 
	{
		$zend_config = Zend_Registry::get("zend_config");
     	$this->errortype = array (
                E_ERROR              => 'Error',
                E_WARNING            => 'Warning',
                E_PARSE              => 'Parsing Error',
            //    E_NOTICE             => 'Notice',
                E_CORE_ERROR         => 'Core Error',
                E_CORE_WARNING       => 'Core Warning',
                E_COMPILE_ERROR      => 'Compile Error',
                E_COMPILE_WARNING    => 'Compile Warning',
                E_USER_ERROR         => 'User Error',
                E_USER_WARNING       => 'User Warning',
                E_USER_NOTICE        => 'User Notice',
                E_STRICT             => 'Runtime Notice',
                E_RECOVERABLE_ERROR  => 'Catchable Fatal Error'
                );
    	//$this->user_errors = $zend_config->error->errortype;
    	$this->logPath = $zend_config->error->errorlog;
    	$this->errordisplay = $zend_config->error->display;
        @set_error_handler(array($this, 'error_handler'));
       
    }
	
    public function error_handler($errtype, $errstr, $errfile, $errline) 
    {
    
      if(array_key_exists($errtype, $this->errortype))
      {
		
			
			if($this->errordisplay == "print")
			$this->displayPrint($errtype, $errstr, $errfile, $errline);
			elseif($this->errordisplay == "log")
			$this->displayLog($errtype, $errstr, $errfile, $errline);
			elseif($this->errordisplay == "all")
			{
			
				$this->displayAll($errtype, $errstr, $errfile, $errline);
			}
      }
		
    }
	public function displayPrint($errtype, $errstr, $errfile, $errline)
	{
		
		//echo "you gong dan!";
		echo "#[$errtype][{$this->errortype[$errtype]}]<br/>
			  ->$errstr<br/>
			  ->($errline)$errfile<br/>";

	}
	public function displayLog($errtype, $errstr, $errfile, $errline)
	{
		$time = date("Y-m-d H:i:s");
		$log = "<->time=$time<>errtype=$errtype<>errstr=$errstr<>errline=$errline<>errfile=$errfile\n";
		error_log($log, 3, $this->logPath.'/log.log');
		$logSql = "<->time=$time\n";
		$sql = L_Recover::readSql();
		if($errtype ==E_USER_ERROR)
		{
			if(!empty($sql))
			{
				foreach($sql as $value)
				{
					$logSql .= "<*>".$value."\n";
				}
			}
			error_log($logSql, 3, $this->logPath.'/logSql.log');
		}
	}
	public function displayAll($errtype, $errstr, $errfile, $errline)
	{
		
		$this->displayPrint($errtype, $errstr, $errfile, $errline);
		$this->displayLog($errtype, $errstr, $errfile, $errline);
		exit;
	}
}
?>